import os
from datetime import datetime
from npy2nc_function import surface, upper

# The date and time of the initial field
date_time = datetime(
    year = 2025, 
    month = 1, 
    day = 1,
    hour = 0,
    minute = 0)
date_time_final = datetime(
    year = 2025,
    month = 1,
    day = 7,
    hour = 1,
    minute = 0)

# The directory for output forecast
output_dir = os.path.join(
    os.path.join(os.getcwd(), "output"),
    ''.join([date_time.strftime("%Y-%m-%d-%H-%M"), "to", date_time_final.strftime("%Y-%m-%d-%H-%M")])
)
# The results for output
result_dir = os.path.join(
        os.path.join(os.getcwd(), "results"),
        date_time.strftime("%Y-%m-%d-%H-%M" + "to" + date_time_final.strftime("%Y-%m-%d-%H-%M"))
)
# create dir if needed
os.makedirs(result_dir, exist_ok=True)

# get all files that need to be decoded
for file in os.listdir(output_dir):
    print(file)
    if file.endswith(".npy"):
        if file.startswith("output_surface"):
            # decode surface data
            surface(os.path.join(output_dir, file), ''.join([file[:-4], ".nc"]), result_dir)
            # os.remove(os.path.join(results_dir, file))
        elif file.startswith("output_upper"):
            # decode upper data
            upper(os.path.join(output_dir, file), ''.join([file[:-4], ".nc"]), result_dir)
            # os.remove(os.path.join(results_dir, file))
                

